/*
 * HeartRateMonitor.cpp
 *
 *  Created on: 28/04/2013
 *      Author: roger
 */

#include "HeartRateMonitor.h"
#include "HeartRateInterrupt.h"

static char *hm_bitmap PROGMEM =
	"!!!!!!!!!!!!!1%\"(\"Q=8FZ?EJ+3I;'BGJK;97&B&R=8!!!!!!!!!!!!!!!!!!!!"
	"!!!!%B)32U>(BI;'IK+CDY_06&25(\"Q=!A)#!!!!!!!!!!!!!!!!!!!!%2%2AI+#"
	"X.T=\\_`P]`0T^04U]`0T\\?XNX>X><'RM\"15&!!!!!!!!\"!1%:76FVN<7[_PL]@(R"
	"^04U]`0T\\O\\OY/$ADY_0&\"15!!!!!!!!!!!!#QM,CIK+\\?XN````````````````"
	"````````````Y_0D:G:G\"Q=(\"15&86V>Y/$A````````````````````````````"
	"]P,SG:G:$1U.!!!!!Q-$>X>X\\/TM````````````````````````````````````"
	"T]`0/DI[/4EZT-T-````````````````````````````````````^04UCIK+\"!1%"
	"&\"15Y/$A````````````````````````````````````````]P,SLK[OI[/D\\?XN"
	"````````````````````````````````````````\\O\\O+#AI7&B9_`P\\````````"
	"````````````````````````````````````_PL[\\_`P_PL[````````````````"
	"`````````````````````````@X^>(2UKKKK`0T]````````````````````````"
	"````````````````````````````````````````````````````````````````"
	"`````````0T]K+CIW>H:`@X^````````````````````````````````````````"
	"`````````````````````````````````````````````````````````@X^Q=(\""
	"Z/4E`P\\_````````````````````````````````````````````````````````"
	"`````````````````````````````````````````P\\_V>86Y/$A`P\\_````````"
	"````````````````````````````````````````````````````````````````"
	"`````````````````````````P\\_X>X>T]`0`@X^````````````````````````"
	"````````````````````````````````````````````````````````````````"
	"`````````@X^Q]0$J[?H`0T]````````````````````````````````````````"
	"`````````````````````````````````````````````````````````0T]L[_P"
	"9W.D_`P\\````````````````````````````````````````````````````````"
	"````````````````````````````````````````_`P\\C)C)+#AI]`0T````````"
	"````````````````````````````````````````````````````````````````"
	"````````````````````````````/$AY#!A)OLK[````````````````````````"
	"````````````````````````````````````````````````````````````````"
	"````````PL[_\"15&!A)#45V.Z/4E````````````````````````````````````"
	"````````````````````````````````````````````````````Z/4E1%\"!!1%\""
	"!!!!#QM,C)C)]@(R````````````````````````````````````````````````"
	"````````````````````````````````]`0TDI[/#QM,!!!!!!!!!!!!'\"A9J[?H"
	"_PL[````````````````````````````````````````````````````````````"
	"````````````_`P\\KKKK'2E:!!!!!!!!!!!!!!!!!!!!(R]@L;WN`@X^````````"
	"`````````````````````````````````````````````````````````@X^L[_P"
	")#!A!!!!!!!!!!!!!!!!!!!!!!!!!!!!(2U>GZO<^P<W````````````````````"
	"````````````````````````````````````^04UF*35'RM<!!!!!!!!!!!!!!!!"
	"!!!!!!!!!!!!!!!!!!!!'2E:BY?(^04U````````````````````````````````"
	"````````````````^@8VC9G*'2E:!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
	"!!!!!!!!&256:'2E_0DY````````````````````````````````````````]0$Q"
	"97&B&B97!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\"Q=("
	"7&B9YO,C````````````````````````````````W>H:56&2\"15&!!!!!!!!!!!!"
	"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!8FZ?P\\_`````"
	"````````````````````Q-$!6666!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
	"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!/DI[Q=(\"````````````````"
	"RM<'0$Q]!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
	"!!!!!!!!!!!!!!!!!!!!!!!!!1%\"-4%RQ]0$````````S-D)0$Q]!A)#!!!!!!!!"
	"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
	"!!!!!!!!!!!!!!!!.D9WW>H:V^@8,CYO!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
	"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
	"$1U.256&1%\"!#QM,!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
	"";

HeartRateMonitor::HeartRateMonitor(): App() {
	// Keep the constructor empty and do most things in the init()
	m_lastReading=0;
}
void HeartRateMonitor::init() {
#ifdef HEARTRATEMONITOR_DEBUG
	Serial.print(getName());Serial.println(INIT);
#endif
	m_icon = new Icon(28,&hm_bitmap[0]);
}
void HeartRateMonitor::setup() {
	Graphics.fillScreen(WHITE);
	Graphics.setRotation(3);
	Graphics.setTextSize(1);

	m_logFile = SD.open(PSTR("datalog.txt"), FILE_WRITE);
	heartRateInterrupt.setLogRawData(true);
	m_effects = false;
	heartRateInterrupt.setEffects(m_effects);
}
boolean HeartRateMonitor::touch(Point p) {
	m_effects = !m_effects;
	heartRateInterrupt.setEffects(m_effects);
	Graphics.fillRect(0,0,Graphics.width(),Graphics.height(),BLACK);
	delay(500);
	Graphics.fillRect(0,0,Graphics.width(),Graphics.height(),WHITE);
	return true;
}
void HeartRateMonitor::display() {
	long currentReading = now()/10; // about every 10 seconds
	if (m_lastReading != currentReading) {
		m_lastReading = currentReading;
		int BPM = heartRateInterrupt.getCurrentBPM();
		float interval = heartRateInterrupt.getLastInterval();
#ifdef HEARTRATEMONITOR_DEBUG
		Serial.print(currentReading);
		Serial.print(COMMA);
		Serial.print(BPM);
		Serial.print(COMMA));
		Serial.println(interval);
#endif
		if (m_logFile != NULL) {
			m_logFile.print(currentReading);
			m_logFile.print(COMMA);
			m_logFile.print(BPM);
			m_logFile.print(COMMA);
			m_logFile.println(interval);
			m_logFile.flush();
		}
	}

	//	Serial.print("pulse,");
//	Serial.println(BPM, 10);
//	Graphics.fillRect(0,Graphics.height()-20,Graphics.width(),20,BLACK);
//	Graphics.setCursor(0,Graphics.height()-10);
//	Graphics.setTextColor(WHITE);
//	Graphics.setTextSize(1);
//	Graphics.print(BPM);
}

void HeartRateMonitor::close() {
	heartRateInterrupt.setLogRawData(false);
	m_logFile.close();
}

HeartRateMonitor::~HeartRateMonitor() {
}

App *heartRateMonitor = new HeartRateMonitor();


